package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;


    @Override
    public List<NursingLevelVo> selectAll() {
        List<NursingLevelVo> nursingLevelVos = nursingLevelMapper.selectAll();
        return nursingLevelVos;
    }

    @Override
    public void insertLevel(NursingLevelDto nursingLevelDto) {
        try {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.insertLevel(nursingLevel);
        }
        catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("信我,你钱不够");
        }
    }

    @Override
    public PageResponse<NursingLevelVo> pageLevel(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingLevel>> page = nursingLevelMapper.pageLevel(pageNum, pageSize, name, status);
        return PageResponse.of(page, NursingLevelVo.class);
    }

    @Override
    public NursingLevelVo getLevelById(Long id) {
        return BeanUtil.toBean(nursingLevelMapper.getLevelById(id), NursingLevelVo.class);
    }

    @Override
    public void updateLevel(NursingLevelDto nursingLevelDto) {
        try {
            NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
            nursingLevelMapper.updateLevel(nursingLevel);
        }
        catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("信我,你钱不够");
        }
    }

    @Override
    public void deleteLevel(Long id) {
        nursingLevelMapper.deleteLevel(id);
    }

    @Override
    public void ture_false(Long id, Integer status) {
        nursingLevelMapper.ture_false(id, status);
    }
}
